/*!
 *
 * 扁平化数组的方法
 *
 */
let treeArr = [[1, [2, [3, 2, [22, [1]]]]], 2, 3, [2, 33, 4, [8, 6, 4, 3]], 2, [2, 2, 8, [2, 13, 32, 12, 21, [22, 3, [[2], 2, 122, [333, 2]]]]]];

function flat(arr) {
  let result = [];
  let unFinished = false;
  let l = arr.length;
  if (l > 0) {
    // 循环数组
    while (l--) {
      // 判断元素类型
      let ele = arr[l];
      if (Object.prototype.toString.call(ele) === '[object Array]') {
        unFinished = true;
        result.push(...ele);
      }
      if (Object.prototype.toString.call(ele) === '[object Number]') {
        result.push(ele);
      }
    }
  }
  return unFinished ? flat(result) : result;
}


function flatB(arr) {
  var ret = []
  var dirty = false
  arr.forEach(item => {
    if (Array.isArray(item)) {
      dirty = true
      ret.push(...item)
    } else {
      ret.push(item)
    }
  })
  return dirty ? flat(ret) : ret
}

// console.time("startAA");
// flatB(treeArr);
// console.timeEnd("startAA");




console.time("startA");
treeArr.flat(Infinity);
console.timeEnd("startA");



